# -*- coding: utf-8 -*-
"""
Module Description:
Date: 2018/4/24
Author: FQ
"""
from itertools import izip_longest


def split_list(a_l, size):
    """
    切分列表
    :param a_l: 需要切分的列表
    :param size: 新列表的大小
    :return: [[],[]]
    """
    new_list = []
    s, e = 0, size
    while s < len(a_l):
        new_list.append(a_l[s:e])
        s += size
        e += size
    return new_list


def list_to_weighted_list(weighted_list):
    """
    区间和概率列表拆分
    :param weighted_list:
    :return: sequence
    eg:[[0.2,0.3,25],[0.31,0.4,25],[0.41,0.5,25]] to [[0.2,0.3],[0.31,0.4],[0.41,0.5]] and [25,25,25]
    """
    sequence = []
    relative_odds = []
    for l in weighted_list:
        if len(l) != 3:
            return False
        sequence.append([l[0], l[1]])
        relative_odds.append(l[2])

    return sequence, relative_odds


def many_element_to_weighted_list(weighted_list):
    """
    多元素list拆分权重或概率
    :param weighted_list:
    :return: sequence
    eg:[[31,1,1,5],[31,2,1,25],[31,3,1,25],[31,4,1,25],[31,5,1,25]] to [[31,1,1],[31,2,1],[31,3,1],[31,4,1]] and [5, 25, 25, 25]
    """
    sequence = []
    relative_odds = []
    for l in weighted_list:
        sequence.append(l[:-1])
        relative_odds.append(l[-1])

    return sequence, relative_odds


def list_to_dict(src_list, dict_key):
    """
    列表转字典，主要目的：减少数据库IO,将数据库结果转化为内存映射进行后续操作。
    :param src_list: 源列表
    :param dict_key: 目标字典key值
    :return: 目标字典
    """
    aim_dict = {}
    if src_list and len(src_list) > 0:
        for item in src_list:
            if hasattr(item, dict_key):
                aim_dict[getattr(item, dict_key)] = item
    return aim_dict


def get_orderly_list(l, reverse=False):
    """
    将list按照reverse规则排序
    将list内dict.values()和set排序
    :param l: list
    :param reverse: False升序
    :return:
    """
    ret_list = list()
    l.sort(reverse=reverse)
    for i in l:
        if isinstance(i, dict):
            values = i.values()
            values.sort(reverse=reverse)
            ret_list.append(values)
        elif isinstance(i, set):
            set_list = list(i)
            set_list.sort(reverse=reverse)
            ret_list.append(set_list)
        else:
            ret_list.append(i)
    return ret_list


def accumulate(iterator):
    """
    累加.python3.2 itertools.accumulate
    """
    total = 0
    for item in iterator:
        total += item
        yield total


def batcher(iterable, n):
    args = [iter(iterable)] * n
    return izip_longest(*args)


if __name__ == '__main__':
    # test_case = list()
    # test_case.append([])
    # test_case.append(None)
    # test_case.append([{'Id': '1', 'Team': 'Spur'}, {'Id': '2', 'Team': 'Lakers'}])
    # test_case.append([{'Id': '1', 'Team': 'Spur'}, {'NotId': '2', 'Team': 'Lakers'}])
    # for case in test_case:
    #     print list_to_dict(case, 'Id')
    # {}
    # {}
    # {'1': {'Id': '1', 'Team': 'Spur'}, '2': {'Id': '2', 'Team': 'Lakers'}}
    # {'1': {'Id': '1', 'Team': 'Spur'}}
    import json
    print isinstance(json.dumps(1), str)
